package hero.mps.web.class_course.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import hero.mps.utils.ResultUtils;
import hero.mps.utils.ResultVo;
import hero.mps.web.class_course.entity.ClassCourse;
import hero.mps.web.class_course.entity.ClassCourseVO;
import hero.mps.web.class_course.entity.ClassTree;
import hero.mps.web.class_course.entity.ParamVO;
import hero.mps.web.class_course.service.ClassCourseService;
import hero.mps.web.course_teacher.entity.CourseTeacher;
import hero.mps.web.course_teacher.service.CourseTeacherService;
import hero.mps.web.school_class.entity.SchoolClass;
import hero.mps.web.school_class.service.SchoolClassService;
import hero.mps.web.school_course.entity.SchoolCourse;
import hero.mps.web.school_course.service.SchoolCourseService;
import hero.mps.web.school_major.entity.SchoolMajor;
import hero.mps.web.school_major.service.SchoolMajorService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/api/classCourse")
@RequiredArgsConstructor
public class ClassCourseController {
    private final ClassCourseService classCourseService;
    private final SchoolMajorService schoolMajorService;
    private final SchoolClassService schoolClassService;
    private final SchoolCourseService schoolCourseService;
    private final CourseTeacherService courseTeacherService;

    /**
     * 查询课程信息
     * @return
     */
    @GetMapping("/getCourseList")
    public ResultVo getCourseList(){
        List<SchoolCourse> list = schoolCourseService.list();
        return ResultUtils.success("查询成功", list);
    }

    /**
     * 删除班级课程信息
     * @param classCourseId
     * @return
     */
    @DeleteMapping("/{classCourseId}")
    public ResultVo deleteCourse(@PathVariable("classCourseId") Integer classCourseId){
        classCourseService.removeById(classCourseId);
        return ResultUtils.success("删除成功");
    }
    /**
     * 获取班级课程信息
     * @param param
     * @return
     */
    @GetMapping("/getClassCourse")
    public ResultVo getClassCourse(ParamVO param){
        IPage<ClassCourseVO> classCourseList = classCourseService.getClassCourseList(param);
        return ResultUtils.success("查询成功", classCourseList);
    }

    /**
     * 保存分配班级课程信息
     * @param classCourseList
     * @return
     */
    @PostMapping("/saveAssignClassCourse")
    public ResultVo saveAssignClassCourse(@RequestBody List<ClassCourse> classCourseList){
        for (int i = 0; i < classCourseList.size(); i++) {
            LambdaQueryWrapper<CourseTeacher> queryWrapper = Wrappers.lambdaQuery(CourseTeacher.class)
                    .eq(CourseTeacher::getCourseId, classCourseList.get(i).getCourseId());
            CourseTeacher courseTeacher = courseTeacherService.getOne(queryWrapper);
            if (courseTeacher != null) {
                classCourseList.get(i).setTeacherId(courseTeacher.getTeacherId());
            }else {
                return ResultUtils.error("请选择已经分配老师的课程");
            }
        }
        boolean saveBatch = classCourseService.saveBatch(classCourseList);
        if (saveBatch){
            return ResultUtils.success("保存分配课程成功");
        }
        return ResultUtils.error("保存分配课程失败");
    }
    /**
     * 获取班级专业数
     * @return
     */
    @GetMapping("/getClassTree")
    public ResultVo getClassTree() {
//        查询所有专业
        QueryWrapper<SchoolMajor> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByAsc(SchoolMajor::getOrderNum);
//        专业列表
        List<SchoolMajor> majorList = schoolMajorService.list(queryWrapper);
//        返回值
        List<ClassTree> vo = new ArrayList<>();
        if (majorList.size() > 0) {
            for (int i = 0; i < majorList.size(); i++) {
                ClassTree tree = new ClassTree();
                Random random = new Random();
                tree.setClassId(majorList.get(i).getMajorId() + random.nextInt());
                tree.setClassName(majorList.get(i).getMajorName());
                tree.setType("0");
//                查询专业下面班级
                QueryWrapper<SchoolClass> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.lambda().eq(SchoolClass::getMajorId, majorList.get(i).getMajorId());
                List<ClassTree> children = new ArrayList<>();
                List<SchoolClass> classList = schoolClassService.list(queryWrapper1);
                if (classList.size() > 0) {
                    for (int j = 0; j < classList.size(); j++) {
                        ClassTree treeVo = new ClassTree();
                        treeVo.setType("1");
                        treeVo.setClassName(classList.get(j).getClassName());
                        treeVo.setClassId(classList.get(j).getClassId());
                        children.add(treeVo);
                    }
                }
                tree.setChildren(children);
                vo.add(tree);

            }
        }
        return ResultUtils.success("查询成功", vo);
    }
}
